package com.jiangtour.controller;

import com.jiangtour.VO.ResultVO;
import com.jiangtour.VO.SiteInfoVO;
import com.jiangtour.VO.UserInfoVO;
import com.jiangtour.converter.OrderForm2OrderDTOConverter;
import com.jiangtour.dataobject.SiteInfo;
import com.jiangtour.dto.OrderDTO;
import com.jiangtour.dto.SiteDTO;
import com.jiangtour.enums.ResultEnum;
import com.jiangtour.enums.SendFlagEnum;
import com.jiangtour.exception.SiteException;
import com.jiangtour.form.OrderForm;
import com.jiangtour.service.CategoryService;
import com.jiangtour.service.ConfigureCategoryService;
import com.jiangtour.service.OrderService;
import com.jiangtour.service.SiteInfoService;
import com.jiangtour.utils.ResultVOUtil;
import com.jiangtour.utils.StrConverterDoubleUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * @author Administrator
 * @Title: SiteClientController
 * @ProjectName site
 * @Description:  客户端控制器
 * @date 2018/12/27/0279:57
 */
@RestController
@RequestMapping("/client")
@Slf4j
public class SiteClientController {
    @Autowired
    private SiteInfoService siteInfoService;
    @Autowired
    private OrderService orderService;


    /**
     *根据经纬度查询学校
     * @param lng 经度
     * @param lat 维度
     * @return
     */
    @GetMapping("/detail")
    public ResultVO detail(@RequestParam(value = "lng", required = false) String lng,
                                     @RequestParam(value = "lat", required = false) String lat,
                                     @RequestParam(value = "siteId", required = false)String siteId) {

        SiteDTO siteDTO = siteInfoService.findSiteByDistance(StrConverterDoubleUtil.strToDouble(lng), StrConverterDoubleUtil.strToDouble(lat), siteId);
        if(siteDTO ==null ){
            throw  new SiteException(ResultEnum.SITE_NOT_EXIST);
        }

        List<SiteInfo> allSite = siteInfoService.findAllSite();
        List<SiteInfoVO> siteInfoVOS = allSite.stream().map(e -> new SiteInfoVO(e.getId(), e.getSiteName())).collect(Collectors.toList());
        if(siteInfoVOS!=null && siteInfoVOS.size()>0){
            siteDTO.setSiteInfoVOList(siteInfoVOS);
        }
        return ResultVOUtil.success( siteDTO);
    }

    /**
     *根据电话号码查询
     * @return
     */
    @GetMapping("/search")
    public ResultVO  list(@RequestParam(value = "phone", required = false) String phone){

        UserInfoVO user = siteInfoService.findUserByPhone(phone);

        return ResultVOUtil.success( user);
    }

    //创建订单
    @PostMapping("/create")
    public ResultVO<Map<String, String>> create(@Valid OrderForm orderForm, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            log.error("【创建订单】参数不正确, orderForm={}", orderForm);
            throw new SiteException(ResultEnum.PARAM_ERROR.getCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        OrderDTO orderDTO = OrderForm2OrderDTOConverter.convert(orderForm);
        if(orderDTO.getAdditional()==null && orderDTO.getOrderType()==SendFlagEnum.SEND.getCode()){
            log.error("【创建订单】,代寄快递收件人不能为空");
            throw  new SiteException(ResultEnum.ADDRESSEE_NOT_EXIST);
        }

        OrderDTO createResult = orderService.create(orderDTO);

        Map<String, String> map = new HashMap<>();
        map.put("orderId", createResult.getOrderId());

        return ResultVOUtil.success(map);
    }


}
